/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */

'use strict';

let esClient = require('./elasticSearch.js');
let metaInfo = require('./articleModel.js');
let BaseEsDao = require('./BaseEsDao.js');
let util = require('util');

function ElasticDao() {
  this.init(metaInfo, esClient);
}
util.inherits(ElasticDao, BaseEsDao);
module.exports = new ElasticDao();
let theProto = ElasticDao.prototype;

theProto.createMany = function (list) {
  var body = [];
  var self = this;
  // 组装bulk对象
  for (var json of list) {
    var action = {
      index: {
        _index: self.index,
        _type: self.type,
        _id: json[self.idFieldName]
      }
    };
    body.push(action, json);
  }
  return self.client.bulk({body: body});
};

theProto.createSingle = function (json) {
  console.log(' [debug] article: createSingle' + JSON.stringify(json));
  this.checkReady();
  let self = this;
  let item = {
    index: self.index,
    type: self.type,
    body: json
  };
  if (json[this.idFieldName]) {
    item.id = json[this.idFieldName];
  }
  return self.client.create(item);
};

theProto.falseDelete = function (json) {
  let partialDoc = {status: 0};
  return this.update(json.tid, {doc: partialDoc});
};

/**
 * 阅读数 + 1
 * @param json
 */
theProto.increaseViews = function (json) {
  let script = 'ctx._source.views += 1';
  return this.update(json.tid, {script: script});
};

/**
 * 点赞 + 1
 * @param json
 */
theProto.increaseLiketimes = function (json) {
  let script = 'ctx._source.liketimes += 1';
  return this.update(json.tid, {script: script});
};

// json { tid:[],status:xx}
theProto.alterStatus = function (json) {
  var partialDoc = {doc: {status: json.status}};
  var body = [];
  var self = this;
  // 组装bulk对象
  for (var oneId of json.tid) {
    var action = {
      update: {
        _index: self.index,
        _type: self.type,
        _id: oneId
      }
    };
    body.push(action, partialDoc);
  }
  return self.client.bulk({body: body});
};

/**
 * 可能update content/cid/ctype等
 * @param json
 */
theProto.updateArticle = function (json) {
  let partialDoc = {};
  if (json.articleContent !== undefined) {
    partialDoc.articleContent = json.articleContent;
  }
  if (json.cid !== undefined) {
    partialDoc.cid = json.cid;
  }
  if (json.ctype !== undefined) {
    partialDoc.ctype = json.ctype;
  }
  if (json.status !== undefined) {
    partialDoc.status = json.status;
  }
  return this.update(json.tid, {doc: partialDoc});
};

theProto.updateReply = function (json) {
  if (json.vid !== null || json.vest === 1) {
    let script = 'ctx._source.replies += 1';
    return this.update(json.tid, {script: script});
  } else {
    let script = 'ctx._source.userreplies += 1';
    return this.update(json.tid, {script: script});
  }
};

theProto.update = function (id, docOrScript) {
  let self = this;
  return this.client.update({
    index: self.index,
    type: self.type,
    id: id,
    body: docOrScript
  });
};


/**
 * 机器匹配
 *
 {
    ctype: 0用户分类,1人工,2机器
    content: xxx,
    top: 4
 }
 * @param json
 * @returns {*}
 */
theProto.autoCate = function*(json) {
  let self = this;
  var body = {
    "query": {
      "filtered": {
        "query": {
          "match": {"articleContent": json.content}
        },
        "filter": {
          "term": {"ctype": json.ctype}
        }
      }
    }
  };

  //if (json.ctype === 'both') {
  //  body.query.filtered.filter = {
  //    "or": [
  //      {"term": {"ctype": "auto"}},
  //      {"term": {"ctype": "mannual"}}
  //    ]
  //  };
  //} else if (json.ctype === 'mannual' || json.ctype === 'auto') {
  //  body.query.filtered.filter = {
  //    "term": {"ctype": json.ctype}
  //  };
  //}
  return this.client.search({
    index: self.index,
    type: self.type,
    from: 0,
    size: json.top,
    body: body
  });
};
